<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2021/9/6
 * Time: 13:59
 */
namespace app\admin\controller;

use app\middleware\AdminCheck;
use think\facade\Db;

class SkillPaper {

    protected $middleware = [
        AdminCheck::class => ['except' 	=> [''] ]
    ];



    /**
     *   @SWG\Post(
     *     path="/admin/skillPaper/add",
     *     tags={"后台考试技巧相关"},
     *     summary="添加技巧等级",
     *     consumes={"application/json"},
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *      in="query",
     *      required=true,
     *      name="city_,title_，sub_title",
     *      type="string",
     *      description="city_（城市）,title_（标题），sub_title（副标题）"),
     *     @SWG\Response(response="200",description="ok")
     *  )
     */
    /**
     * 添加技巧等级
     * @return \think\response\Json
     */
    public function add(){
        validateParam('city_,title_,sub_title','city_,title_,sub_title');
        $data['city_']=input('city_');
        $data['title_']=input('title_');
        $data['sub_title']=input('sub_title');
        Db::name('skill_paper')->insert($data);
        return success('添加成功');
    }




    /**
     *   @SWG\Post(
     *     path="/admin/skillPaper/getList",
     *     tags={"后台考试技巧相关"},
     *     summary="技巧等级列表",
     *     consumes={"application/json"},
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *      in="query",
     *      required=true,
     *      name="pageNo,pageSize",
     *      type="string",
     *      description="pageNo(第几页),pageSize（一页多少条数据）,city_（城市）"),
     *     @SWG\Response(response="200",description="ok")
     *  )
     */
    /**
     * 章节列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(){
        validateParam('city_,pageNo,pageSize','city_,pageNo,pageSize');
        $param=input();
        $where[]=['city_','=',$param['city_']];
        //获取总数
        $total=Db::name('skill_paper')
            ->where($where)
            ->count();
        $offset=makeOffset($param['pageNo'],$param['pageSize']);

        $list=Db::name('skill_paper')
            ->where($where)
            ->limit($offset,$param['pageSize'])
            ->select();
        return success('技巧等级列表',['total'=>$total,'list'=>$list]);
    }



    /**
     *   @SWG\Post(
     *     path="/admin/skillPaper/edit",
     *     tags={"后台考试技巧相关"},
     *     summary="编辑技巧等级",
     *     consumes={"application/json"},
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *      in="query",
     *      required=true,
     *      name="id_,city_,title_，sub_title",
     *      type="string",
     *      description="id_,city_（城市）,title_（标题），sub_title（副标题）"),
     *     @SWG\Response(response="200",description="ok")
     *  )
     */
    /**
     * 编辑技巧等级
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function edit(){
        validateParam('city_,title_,sub_title','city_,title_,sub_title');
        $data['city_']=input('city_');
        $data['title_']=input('title_');
        $data['sub_title']=input('sub_title');
        Db::name('skill_paper')
            ->where('id_',input('id_'))
            ->update($data);
        return success('编辑成功');
    }




    /**
     *   @SWG\Post(
     *     path="/admin/skillPaper/delete",
     *     tags={"后台考试技巧相关"},
     *     summary="删除章节（单个删除）",
     *     consumes={"application/json"},
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *      in="query",
     *      required=false,
     *      name="id_",
     *      type="string",
     *      description="id_"),
     *     @SWG\Response(response="200",description="ok")
     *  )
     */
    /**
     * 删除技巧等级
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function delete(){
        validateParam('id_','id_');
        //首先判断技巧等级下面是否存在技巧章节
        $chapter = Db::name('skill_chapter')
            ->where('skill_paper_id',input('id_'))
            ->find();
        if(!empty($chapter)){
            return error('技巧等级下存在技巧章节，不可删除');
        }
        Db::name('skill_paper')
            ->where('id_','=',input('id_'))
            ->delete();
        return success('删除成功');
    }



}